import MineService from '../../mine/service';
import { LoginModel } from '../model';
import LoginService from '../service';

export default class LoginManager {

	shareUserId = '';

	private static instance : LoginManager;
	private constructor() {

	}

	public static getInstance() : LoginManager {
		if (!LoginManager.instance) {
			LoginManager.instance = new LoginManager();
		}
		return LoginManager.instance;
	}

	public isLogin() : boolean {
		var loginData = new LoginModel().loadCache();
		console.log("userLoginModel === ", loginData);
		if (loginData && loginData.token) {
			return true;
		}
		uni.navigateTo({
			url: "/pages/login/index"
		});
		return false;
	}

	public token() : string {
		var loginData = new LoginModel().loadCache();
		console.log("userLoginModel === ", loginData);
		if (loginData && loginData.token) {
			const cacheData = new LoginModel().loadCache();
			if (!cacheData) {
				console.log("用户数据为空 准备获取");
				new MineService().getUserInfo()
			}
			return loginData.token;
		}
		return '';
	}

	public logout() : void {
		new LoginService().logout()
		new LoginModel().clearCache();
		uni.removeStorageSync('token');
	}
}